package cn.com.zhbs.netty.springListener;

import cn.com.zhbs.netty.EchoServer;
import cn.com.zhbs.netty.utils.SysConfig;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextClosedEvent;
import org.springframework.stereotype.Service;
import org.springframework.web.context.WebApplicationContext;

import javax.persistence.criteria.Root;

@Service
public class CloseNettyListener implements ApplicationListener<ContextClosedEvent> {
    private final static int autoStart = Integer.parseInt(SysConfig.getInstance().getProperty("autoStart"));

    // 在Spring容器被销毁前执行以下内容
    @Override
    public void onApplicationEvent(ContextClosedEvent event) {
        System.out.println(event.getApplicationContext().getDisplayName());
        if (event.getApplicationContext().getDisplayName().equals("Root WebApplicationContext")){
            if (autoStart==1){
                EchoServer.stop();
            }
        }
    }
}
